<?php
namespace app\admin\controller;
class Useraddress extends Base
{
    //列表
    public function lists(){
        if(is_post()){
            $d = $this->post;
            $map = [];
            if($d['keyword']!=''){
                $map[] = ['and',"address_name|address_phone|address","like","%{$d['keyword']}%"];
            }
            $w  = where($map);
            $sql = "select a.*,b.username from user_address a left join user b on a.user_id=b.id  $w[0] order by id desc".limit($d['page'],$d['limit'], $w[1]);
            $rs = $this->db->query($sql, $w[1],'',true);
            if($rs){
                suc($rs[0],$rs[1]);
            }else{
                suc([],0);
            }
        }
        $this->fetch();
    }
    //编辑
    public function del(){
      if(is_post()){
        $id = intval($this->post["id"]);
        $rs = $this->db->delete("user_address",$id);
        if($rs){
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
        }else{
            return err("删除失败");
        }
      }

    }

    #批量删除
    function delall(){
      if(is_post()){
            $d = $this->post;
            $ids = $d['id'];
            foreach($ids as $id){
                $this->db->delete('user_address',$id);
            }
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
      }
    }
}
